package com.csdj.controller;

import com.csdj.query.UserQuery;
import com.csdj.service.UserService;
import com.csdj.utils.ResultInfo;
import com.csdj.vo.User;
import com.csdj.vo.Users;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Controller
@Api(tags = "用户管理模块")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * restful 查询用 @GetMapping
     * 添加 用 @PostMapping
     * 修改 用 @PutMapping
     * 删除 用 @DeleteMapping
     * @param uname
     * @return
     */
    @GetMapping("/user/{uname}")
    @ResponseBody
    @ApiOperation(value = "用户管理--根据用户名查询用户信息",notes = "用户模块")
    @ApiImplicitParam(name = "uname",value = "用户名",required = true,paramType = "path")
    public User queryUserByName(@PathVariable("uname") String uname){
        User user = userService.queryUserByName(uname);

        return user;

    }

    @GetMapping("/users/{id}")
    @ResponseBody
    @ApiOperation(value = "用户管理--根据用户id查询用户信息",notes = "用户模块")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "path")
    public User queryUserById(@PathVariable Integer id){

        return userService.queryUserById(id);
    }

    /**
     * @RequestBody 前端页面 传值 过来 接收的是 json格式
     * from-data 表单数据 不需要加 @RequestBody
     * @param user
     * @return
     */
    @PostMapping("/user/add")
    @ResponseBody
    @ApiOperation(value = "用户管理--添加用户信息",notes = "用户模块")
    public ResultInfo save(User user){
        ResultInfo resultInfo=new ResultInfo();
        try {
            userService.save(user);
            //添加 成功!
            resultInfo.setCode(200);
            resultInfo.setMsg("添加成功!");
        } catch (Exception e) {
            resultInfo.setCode(300);
            resultInfo.setMsg("添加失败!");
            e.printStackTrace();
        }
        return resultInfo;
    }

    @PutMapping("/user/update")
    @CacheEvict(value = "users",key = "#user.id")
    @ResponseBody
    @ApiOperation(value = "用户管理--用户更新",notes = "用户模块")
    public ResultInfo updateUser(@RequestBody User user){
        ResultInfo resultInfo=new ResultInfo();
        try {
            userService.update(user);
            resultInfo.setCode(200);
            resultInfo.setMsg("修改成功！");
        } catch (Exception e) {
            resultInfo.setCode(300);
            resultInfo.setMsg("修改失败！");
            e.printStackTrace();
        }
        return  resultInfo;
    }

    @DeleteMapping("/delete/{id}")
    @CacheEvict(value = "users",allEntries = true)
    @ResponseBody
    @ApiOperation(value = "用户管理--根据id删除用户信息",notes = "用户模块")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "path")
    public ResultInfo deleteUser(@PathVariable Integer id){
        ResultInfo resultInfo=new ResultInfo();
        try {
            userService.delete(id);
            resultInfo.setCode(200);
            resultInfo.setMsg("删除成功!");
        } catch (Exception e) {
            resultInfo.setCode(300);
            resultInfo.setMsg("删除失败!");
            e.printStackTrace();
        }
        return resultInfo;
    }

    @GetMapping("user/list")
    @ResponseBody
    @ApiOperation(value = "用户管理--分页显示用户信息",notes = "用户模块")
    public PageInfo<User> queryByParams(@RequestBody UserQuery userQuery){

        PageInfo<User> pageInfo = userService.queryUserByParams(userQuery);

        return pageInfo;
    }



    @RequestMapping("/index")
    public String index(){
        System.out.println("hello world");
        System.out.println("hello====");
        System.out.println("===========");
        return "index";
    }

    @RequestMapping("/users")
    @ResponseBody
    public ResultInfo showUser(@Valid Users users){

        System.out.println(users);
        ResultInfo resultInfo=new ResultInfo();

        return resultInfo;
    }


}
